from flask import Flask, render_template, request, json, jsonify
from flask_bootstrap import Bootstrap
import sqlite3
import time

app = Flask(__name__)
bootstrap = Bootstrap(app)


# 连接数据库
def conn_sqlite():
    conn = sqlite3.connect('db_test.sqlite3')
    cur = conn.cursor()
    return conn, cur


# 插入数据
def insert_sqlite(sql):
    conn, cur = conn_sqlite()
    cur.execute(sql)
    conn.commit()


# 查询数据
def select_sqlite(sql):
    conn, cur = conn_sqlite()
    cur.execute(sql)
    result = cur.fetchall()
    record = []
    for i in result:
        record.append(i)
    return record


@app.route('/')
def open():
    return render_template('login.html')


@app.route('/recordList', methods=['POST', 'GET'])
def recordList():
    if request.method == 'POST':
        requestDict = request.form.to_dict()
        username = requestDict.get('username')
        password = requestDict.get('password')
        idCard = requestDict.get('idCard')
        phone = requestDict.get('phone')
        sql = "insert into userinfor values (\'%s\',\'%s\',\'%s\',\'%s\')" % (username, password, idCard, phone)
        insert_sqlite(sql)
    return render_template('login.html')


@app.route('/index/', methods=['POST', 'GET'])
def index():
    if request.method == "POST":
        requestDict = request.form.to_dict()
        global username
        username = requestDict.get('username')
        password = requestDict.get('password')
        sql = "select username,password from userinfor where username=\'%s\'" % username
        us_pwd = select_sqlite(sql)[0]
        if username == us_pwd[0] and password == us_pwd[1]:
            return render_template('index.html', message="欢迎" + username + "登录主页")
        else:
            return render_template('login.html', message="用户名或密码错误！")


@app.route('/register', methods=['POST', 'GET'])
def register():
    return render_template('register.html')


@app.route('/buy', methods=['POST', 'GET'])
def buy():
    sql = "select * from fruitPrice"
    p = select_sqlite(sql)
    return render_template('shopping.html', p=p[0])


@app.route('/formcheck', methods=['POST', 'GET'])
def formcheck():
    FruitFrom = request.form.to_dict()
    weights_Tur = FruitFrom.values()  # 这是元组类型
    weights_List = list(weights_Tur)  # 转换成列表
    FromRecordList = []
    for i in weights_List:
        I = []
        I.append(float(i))
        FromRecordList.append(I)
    Price = [2.2, 3.3, 4.4, 5.5, 6.6, 7.7]
    FruitName = ["苹果", "梨", "香蕉", "橙子", "荔枝", "柚子"]
    global amount
    money = 0
    for i in range(len(FromRecordList)):
        FromRecordList[i].insert(0, FruitName[i])
        FromRecordList[i].insert(1, Price[i])
        money = money + FromRecordList[i][1] * FromRecordList[i][2]
    amount = money
    for i in range(len(FromRecordList)):
        sql = "insert into FruitForm values (\'%s\', null ,\'%s\',\'%s\',\'%s\',\'%s\')" \
              % (username, FromRecordList[i][0], FromRecordList[i][1], FromRecordList[i][2],
                 FromRecordList[i][1] * FromRecordList[i][2])
        insert_sqlite(sql)
    return render_template('personal_information.html', amount=amount)


@app.route('/formInfor', methods=['POST', 'GET'])
def formInfor():
    if request.method == 'POST':
        dateTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
        print(dateTime)
        formInforDict = request.form.to_dict()
        consignee = formInforDict.get('consignee')
        address = formInforDict.get('address')
        phone = formInforDict.get('phone')
        sql = "insert into orderForm values (null,\'%s\',\'%s\',\'%s\',\'%s\',\'%s\',\'%s\')" % (
            username, consignee, dateTime, address, phone, amount)
        insert_sqlite(sql)
    return render_template('shopEnd.html')


if __name__ == '__main__':
    app.run()
